<?php

namespace web\user\controller;

use addons\member\model\MemberAccountModel;

/**
 * 用户登录控制器
 */
class Login extends \web\common\controller\BaseController {

    public function index() {
        if (IS_POST) {
            /* 检测验证码 */
            $code = $this->_post('code');
            if (!captcha_check($code)) {
                return $this->failData('验证码输入错误！');
            }
            //0=总管理,1=作者,2=主播,3=商家
            $user_type = $this->_post('user_type');
            $username = $this->_post('username');
            $password = $this->_post('password');
            // $verify_code = $this->_post('verify_code');
            // if (empty($verify_code)){
            //     return $this->failData('请输入验证码');
            // }
            if (empty($username)) {
                return $this->failData('账号不能为空');
            }
            if (empty($password)) {
                return $this->failData('密码不能为空');
            }

            //短信验证码验证
            // $type = 10;
            // $country_code  = 86;
            // $phone = '17706915880';
            // $verifyM = new \addons\member\model\VericodeModel();
            // $_verify = $verifyM->VerifyCode($verify_code, $phone, $type, $country_code);
            // if (empty($_verify)) {
            //     // return $this->failData('短信验证码有误');
            // }

            if ($user_type == 0) {
                $m = new \web\common\model\user\AccountModel();
                $res = $m->getLoginData($username, $password);
                if ($res) {
                    if ($res['logic_delete'] == 1)
                        return $this->failData('账号已删除，您的帐号无法登录');

                    $loginData = array('uid' => $res['id']);
                    $addons_ids = 0;
                    if ($res['is_admin'] == 0) {
                        $roleM = new \addons\config\model\Role();
                        $addons_ids = $roleM->getDataByID($res['role_id'], 'addon_ids');
                        $menuM = new \web\common\model\sys\UserNavMenuModel();
                        $loginData['promission'] = $menuM->getControllerByIDS($addons_ids);
                    }
                    $loginData['username'] = $res['username'];
                    $loginData['is_admin'] = $res['is_admin'];
                    $loginData['store_id'] = 0;
                    $loginData['role_id'] = $res['role_id'];
                    $loginData['addons_ids'] = $addons_ids;
                    $loginData['is_frozen'] = 1;
                    session('loginData', $loginData);
                } else {
                    return $this->failData('帐号或密码有误');
                }
            } else if ($user_type == 1) {
                //作者账号
                $accountM = new MemberAccountModel();
                $user_data = $accountM->where(['username' => $username, 'password' => md5($password)])->find();
                if(empty($user_data)) return $this->failData('密码错误');

                $storeM = new \addons\shop\model\Store();
                $store_id = $storeM->where(['user_id' => $user_data['id'], 'logic_delete' => 0,'check_state' => 1])->value('id');
                if(!$store_id) return $this->failData('您不是商户');

                $loginData = array('uid' => $user_data['id']);
                $loginData['username'] = $username;
                $loginData['store_id'] = $store_id;
                $loginData['addons_ids'] = 0;
                $loginData['is_admin'] = 0;
                $loginData['is_frozen'] = 1;

            } else {
                return $this->failData('账号类型错误');
            }
            $loginData['user_type'] = $user_type;
            session('loginData', $loginData);
            $url = url('index/index');
            return $this->successData($url);
        } else {
            return $this->fetch();
        }
    }

    /**
     * 退出
     */
    public function logout() {
        $loginData = session('loginData');
        if (empty($loginData))
            return $this->successData();
        session('loginData', null);
        return $this->successData();
    }
    /*
     * 获取验证码
     */
    public function getCode()
    {
        $country_code = '86';
        $phone = '17706915880';
        $time = 120;
        $type = 10; //后台登录验证码
        $m = new \addons\member\model\VericodeModel();
        $unpass_code = $m->hasUnpassCode($phone, $type, $country_code);
        if (!empty($unpass_code)) {
            return $this->failData(lang('API_CODE_EXISTS'));
        }
        try {
            //发送验证码 $country_code .
            $res = \addons\member\utils\ChSms::send($phone);
            if (!$res['success']) {
                return $this->failData($res['message']);
            }
            //保存验证码
            $pass_time = date('Y-m-d H:i:s', strtotime("+" . $time . " seconds"));
            $data['country_code'] = $country_code;
            $data['phone'] = $phone;
            $data['code'] = $res['code'];
            $data['type'] = $type;
            $data['pass_time'] = $pass_time; //过期时间
            $result = $m->add($data);
            if (empty($result)) {
                return $this->failData(lang('API_CODE_FAIL')); //写入数据库失败
            }
            unset($res['code']);
            return $this->successData($res['message']);
        } catch (\Exception $ex) {
            return $this->failData($ex->getMessage());
        }
    }
    /**
     * 输出错误JSON信息。
     * @param type $message
     */
    protected function failJSON($message) {
        $message = lang($message);
        $jsonData = array('success' => false, 'message' => $message);
        $json = json_encode($jsonData, true);
        echo $json;
        exit;
    }

    /**
     * 输出成功JSON信息
     * @param type $data
     */
    protected function successJSON($data = NULL, $msg = "success") {
        $jsonData = array('success' => true, 'data' => $data, 'message' => $msg);
        $json = json_encode($jsonData, 1);
        echo $json;
        exit;
    }
}
